package com.cloudptt.api.product.func.lbs;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.SystemClock;

import com.cloudptt.api.product.log.Log;

public class AlarmTimer extends BroadcastReceiver {
	public final static String THIS_FILE = "AlarmTimer";
	
//	public static final String ALARM_TIMER_ACTION="repeating";
	public static final String KEY_TIME_INTERVAL="time_interval";
	public static final String KEY_MCPTT_ID="mcptt_id";

	private Context ctx = null;
	private String alarmAction = null;
	private int refreshtime = 10;
	private IAlarmTimeout timout = null;
	private AlarmManager am= null;


	public AlarmTimer(Context ctx,String alarmAction,int refreshtime,IAlarmTimeout timout) {
		Log.i(LBSFunc.Log_Key, "AlarmTimer alarmAction = " + alarmAction);
		this.ctx = ctx;
		this.alarmAction = alarmAction;
		this.refreshtime = refreshtime;
		this.timout = timout;
		ctx.registerReceiver(this, new IntentFilter(alarmAction));
	}


	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.d(THIS_FILE, "onReceive:Action is " + action);
		if (alarmAction != null
				&& alarmAction.equals(action)) 
		{
			if(intent != null)
			{
//				int val = intent.getIntExtra(KEY_TIME_INTERVAL, 10);
				if(timout != null)
				{
					timout.timeIntervalTimeout(alarmAction);
				}
				
				if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 
				{
					startRefreshTimer();
				}
			}
		}
	}

	public void startRefreshTimer() 
	{
		if(ctx == null)
		{
			return;
		}
		
		Log.i(THIS_FILE, "startRefreshTimer refreshtime = " + refreshtime + " alarmAction = " + alarmAction);
		Intent intent =new Intent(alarmAction);
//		intent.putExtra(KEY_TIME_INTERVAL, refreshtime);
		PendingIntent sender=PendingIntent.getBroadcast(ctx, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

		this.am=(AlarmManager)ctx.getSystemService(Service.ALARM_SERVICE);
		
//		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
//		{
//			am.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + refreshtime*1000, sender);
//		}
//		else
			if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
		{
			am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + refreshtime*1000, sender);
		} 
		else 
		{
			am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), refreshtime*1000, sender);
		}
	}
	

	
	public void stopRefreshTimer()
	{
		Log.d(THIS_FILE, "stopRefreshTimer alarmAction = " + alarmAction);
		Intent intent =new Intent(alarmAction);
		PendingIntent sender=PendingIntent.getBroadcast(ctx, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
		AlarmManager alarm=(AlarmManager)ctx.getSystemService(Service.ALARM_SERVICE);
		alarm.cancel(sender);
		
		clearResource();
	}
	
	public void clearResource() {
		Log.d(THIS_FILE, "AlarmTimer clearResource");
		this.ctx.unregisterReceiver(this);
		this.ctx = null;
		this.timout = null;
	}

}
